﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ZEBuz.Questionnaire.Lib.Util
{
    public static class Util
    {
        public static int ToInt(this string str, int defaultVal = default(int))
        {
            if (string.IsNullOrEmpty(str)) { return defaultVal; }

            int ret = defaultVal;
            int.TryParse(str, out ret);
            return ret;
        }

        public static string ToString(this object str, string defaultVal = default(string))
        {
            if (str == DBNull.Value) { return defaultVal; }
            return str.ToString();
        }

        public static DateTime ToDateTime(this string str, DateTime defaultVal = default(DateTime)) 
        {
            DateTime ret = defaultVal;
            DateTime.TryParse(str.ToString(), out ret);
            return ret;
        }

        public static T ToEnum<T>(this string str, T defaultVal = default(T)) where T : struct
        { 
            T ret = defaultVal;
            Enum.TryParse<T>(str, out ret);
            return ret;
        }

        public static bool FromDatabaseBit(this string str, bool defaultVal = false)
        {
            if (str == "1") return true;
            else return false;
        }
    }
}
